Go back to the About page.
Let us set some global options for all code chunks in this document.
knitr::opts_chunk$set(
message = FALSE, # Disable messages printed by R code chunks
warning = FALSE, # Disable warnings printed by R code chunks
echo = TRUE, # Show R code within code chunks in output
include = TRUE, # Include both R code and its results in output
eval = TRUE, # Evaluate R code chunks
cache = FALSE, # Enable caching of R code chunks for faster rendering
fig.align = "center",
out.width = "100%",
retina = 2,
error = TRUE,
collapse = FALSE
)
rm(list = ls())
set.seed(1982)Follow this link for an illustration of the geodesic distance on a metric graph.
Follow this link for an illustration of a function on a metric graph.
Follow this link for alternative illustrations of basis functions on a metric graph.
Let each edge \(e\in\mathcal{E}\) be subdivided into \(n_{e}\geq 2\) regular segments of length \(h_{e}\), and be delimited by the nodes \(0 = x_0^{e},x_1^{e},\dots,x_{n_{e}-1}^{e}, x_{n_{e}}^{e} = \ell_{e}\). For each \(j = 1,\dots,n_{e}-1\), we consider the following standard hat basis functions \[\begin{equation*} \varphi_j^{e}(x)=\begin{cases} 1-\dfrac{|x_j^{e}-x|}{h_{e}},&\text{ if }x_{j-1}^{e}\leq x\leq x_{j+1}^{e},\\ 0,&\text{ otherwise}. \end{cases} \end{equation*}\] For each \(e\in\mathcal{E}\), the set of hat functions \(\left\{\varphi_1^{e},\dots,\varphi_{n_{e}-1}^{e}\right\}\) is a basis for the space \[\begin{equation*} V_{h_{e}} = \left\{w\in H_0^1(e)\;\Big|\;\forall j = 0,1,\dots,n_{e}-1:w|_{[x_j^{e}, x_{j+1}^{e}]}\in\mathbb{P}^1\right\}, \end{equation*}\] where \(\mathbb{P}^1\) is the space of linear functions on \([0,\ell_{e}]\). For each vertex \(v\in\mathcal{V}\), we define \[\begin{equation*} \mathcal{N}_v = \left\{\bigcup_{e\in\left\{e\in\mathcal{E}_v: v = x_0^e\right\}}[v,x_1^e]\right\}\bigcup\left\{\bigcup_{e\in\left\{e\in\mathcal{E}_v: v = x^e_{n_e}\right\}}[x^e_{n_e-1},v]\right\}, \end{equation*}\] which is a star-shaped set with center at \(v\) and rays made of the segments contiguous to \(v\). On \(\mathcal{N}_v\), we define the hat functions as \[\begin{equation*} \phi_v(x)=\begin{cases} 1-\dfrac{|x_v^{e}-x|}{h_{e}},&\text{ if }x\in\mathcal{N}_v\cap e \text{ and }e\in\mathcal{E}_v,\\ 0,&\text{ otherwise}, \end{cases} \end{equation*}\] where \(x_v^e\) is either \(x_0^e\) or \(x_{n_e}^e\) depending on the edge direction and its parameterization. See Arioli and Benzi (2018) for more. Figure 3 below provides an illustration of the system of basis functions \(\{\varphi_j^e, \phi_v\}\) together with the set \(\mathcal{N}_v\).
Press the Show button below to reveal the code.
# star set
d49 = data.frame(y = c(1/3,0), x = c(0,0), z = c(0,0))
d50 = data.frame(y = c(0,0), x = c(1/3,0), z = c(0,0))
d51 = data.frame(y = c(-1/3,0), x = c(0,0), z = c(0,0))
d52 = data.frame(y = c(0,0), x = c(-1/3,0), z = c(0,0))
graph_to_get_loc <- graph$clone()
graph_to_get_loc$build_mesh(h = 1/120)
loc <- graph_to_get_loc$get_mesh_locations()
graph$build_mesh(h = 1/3)
A <- graph$fem_basis(loc)
DD <- data.frame(A = as.matrix(A)) %>%
rowwise() %>%
mutate(max_value = max(c_across(everything()))) %>%
cbind(edge_number = loc[, 1], distance_on_edge = loc[, 2], A.24 = 0)
graph$add_observations(data = DD, normalized = TRUE)
# Initialize the first plot
p <- graph_to_get_loc$plot_function(X = DD[[paste0("A.", 1)]], line_color = "red",
edge_width = 2, line_width = 2, support_width = 0.2, plotly = TRUE)
# Loop through to create the remaining plots
for (i in 2:24) {
line_color <- "gray"
if (i == 11) {line_color <- "blue"}
if (i == 24) {line_color <- "black"}
X <- DD[[paste0("A.", i)]]
p <- graph_to_get_loc$plot_function(X = X, line_color = line_color, edge_width = 2, line_width = 2, support_width = 0.2, plotly = TRUE, p = p)
}
pp = p %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\text{Basis functions on a metric graph }\\Gamma = (\\mathcal{V},\\mathcal{E})"),
showlegend = FALSE,
font = list(family = "Palatino"),
scene = list(
aspectratio = list(x = 1.8, y = 1.8, z = 0.4),
annotations = list(
list(
x = -1, y = 0, z = 0,
text = TeX("v_1"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 0, z = 0,
text = TeX("v_2"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = -1, z = 0,
text = TeX("v_3"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = -1, z = 0,
text = TeX("v_4"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 0, z = 0,
text = TeX("v_5"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1, y = 1, z = 0,
text = TeX("v_6,x_0^{e_7}"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 1, z = 0,
text = TeX("v_7,x_3^{e_7}"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 2/3, y = 1, z = 0,
text = TeX("x_1^{e_7}"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 1/3, y = 1, z = 0,
text = TeX("x_2^{e_7}"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "black", size = 16),
arrowcolor = "black", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0.15, y = 0, z = 0,
text = TeX("\\mathcal{N}_{v_2}"),
textangle = 0, ax = 0, ay = 25,
font = list(color = "green", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 2/3, y = 1, z = 1,
text = TeX("\\varphi_1^{e_7}"),
textangle = 0, ax = 0, ay = 75,
font = list(color = "blue", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1),
list(
x = 0, y = 0, z = 1,
text = TeX("\\phi_{v_2}"),
textangle = 0, ax = 0, ay = 75,
font = list(color = "red", size = 16),
arrowcolor = "white", arrowsize = 1, arrowwidth = 1,arrowhead = 1)
))) %>%
add_trace(x = 0, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 0, y = 1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1, y = 1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1, y = -1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 0, y = -1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = -1, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = -2/3, y = 0, z = 0, mode = "markers", type = "scatter3d", # before this
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = -1/3, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 0, y = -1/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 0, y = -2/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1/3, y = -1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 2/3, y = -1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 0, y = 1/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 0, y = 2/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1/3, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 2/3, y = 0, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1/3, y = 1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 2/3, y = 1, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1, y = 1/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1, y = 2/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1, y = -1/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(x = 1, y = -2/3, z = 0, mode = "markers", type = "scatter3d",
marker = list(size = 4, color = "blue", symbol = 104)) %>%
add_trace(data = d49, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 3, color = "green", symbol = 104)) %>%
add_trace(data = d50, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 3, color = "green", symbol = 104)) %>%
add_trace(data = d51, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 3, color = "green", symbol = 104)) %>%
add_trace(data = d52, x = ~x, y = ~y, z = ~z, mode = "lines", type = "scatter3d",
line = list(width = 3, color = "green", symbol = 104))
ppFigure 3. Illustration of the system of basis functions \(\{\varphi_j^e, \phi_v\}\) (solid gray lines) on the graph \(\Gamma= (\mathcal{V},\mathcal{E})\), where \(\mathcal{E}= \left\{e_1,\dots,e_8\right\}\) and \(\mathcal{V}= \left\{v_1,\dots,v_7\right\}\). Note that for all \(e_i\in\mathcal{E}\), \(n_{e_i} = 3\) and \(h_{e_i} = 1/3\). In particular, for edge \(e_7\), observe that the three regular segments are delimited by the nodes \(v_6 = x_0^{e_7}, x_1^{e_7}, x_2^{e_7}, x_3^{e_7} = v_7\). Corresponding to node \(x_1^{e_7}\), we have plotted the basis function \(\varphi_1^{e_7}\) in blue. Notice also that the set \(\mathcal{N}_{v_2}\) is depicted in green and its corresponding basis function \(\phi_{v_2}\) is shown in red.
We used R version 4.4.0 (R Core Team 2024) and the following R packages: ggtext v. 0.1.2 (Wilke and Wiernik 2022), gridExtra v. 2.3 (Auguie 2017), here v. 1.0.1 (Müller 2020), htmltools v. 0.5.8.1 (Cheng et al. 2024), INLA v. 24.6.27 (Rue, Martino, and Chopin 2009; Lindgren, Rue, and Lindström 2011; Martins et al. 2013; Lindgren and Rue 2015; De Coninck et al. 2016; Rue et al. 2017; Verbosio et al. 2017; Bakka et al. 2018; Kourounis, Fuchs, and Schenk 2018), inlabru v. 2.10.1.9010 (Yuan et al. 2017; Bachl et al. 2019), knitr v. 1.47 (Xie 2014, 2015, 2024), latex2exp v. 0.9.6 (Meschiari 2022), listviewer v. 4.0.0 (de Jong, Gainer, and Russell 2023), mapview v. 2.11.2 (Appelhans et al. 2023), Matrix v. 1.6.5 (Bates, Maechler, and Jagan 2024), MetricGraph v. 1.3.0.9000 (Bolin, Simas, and Wallin 2023b, 2023a, 2023c, 2024; Bolin et al. 2023), patchwork v. 1.2.0 (Pedersen 2024), plotly v. 4.10.4 (Sievert 2020), plotrix v. 3.8.4 (J 2006), reshape2 v. 1.4.4 (Wickham 2007), rmarkdown v. 2.27 (Xie, Allaire, and Grolemund 2018; Xie, Dervieux, and Riederer 2020; Allaire et al. 2024), rSPDE v. 2.3.3.9000 (Bolin and Kirchner 2020; Bolin and Simas 2023; Bolin, Simas, and Xiong 2023), scales v. 1.3.0 (Wickham, Pedersen, and Seidel 2023), sf v. 1.0.16 (Pebesma 2018; Pebesma and Bivand 2023), tidyverse v. 2.0.0 (Wickham et al. 2019), TSstudio v. 0.1.7 (Krispin 2023), xaringanExtra v. 0.8.0 (Aden-Buie and Warkentin 2024).